package com.block.datapush.service.apipush.utils;

import cn.hutool.core.util.StrUtil;
import com.block.common.enums.BaseTypeInfoEnum;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 乐享通
 */
public class LeXiangTongUtil {

    public static String getXueLi(String xueli) {
        if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl001.getValue(), xueli) || StrUtil.equals(BaseTypeInfoEnum.XueLi.xl002.getValue(), xueli)) {
            return "1";
        }else if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl003.getValue(), xueli)){
            return "2";
        }else if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl004.getValue(), xueli)){
            return "3";
        }
        return "4";
    }

    public static Integer getGongZuo(String gongzuo) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz001.getValue(), gongzuo)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz003.getValue(), gongzuo)) {
            return 3;
        }
        return 2;
    }

    public static Integer getGongZuoType(String gongzuo) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz002.getValue(), gongzuo)) {
            return 6;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz003.getValue(), gongzuo)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz004.getValue(), gongzuo)) {
            return 4;
        }
        return 2;
    }

    public static Integer getGjj(String gongjijin) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj001.getValue(), gongjijin)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj002.getValue(), gongjijin)){
            return 2;
        }
        return 3;
    }

    public static Integer getshebao(String shebao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb001.getValue(), shebao)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb002.getValue(), shebao)) {
            return 2;
        }
        return 3;
    }

    public static Integer getHouse(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.House.hu003.getValue(), house)){
            return 3;
        }
        return 2;
    }

    public static Integer getCar(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Car.car002.getValue(), car)){
            return 3;
        }
        return 2;
    }

    public static Integer getXinYongKa(String xinyongka) {
        if (StrUtil.equals(BaseTypeInfoEnum.XinYongKa.xyk001.getValue(), xinyongka)) {
            return 1;
        }
        return 4;
    }

    public static Integer getbaodan(String baodan) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baoxian.bx001.getValue(), baodan)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Baoxian.bx002.getValue(), baodan)) {
            return 2;
        }
        return 3;
    }

    public static Integer getloan(String loan) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loan)) {
            return 30000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la002.getValue(), loan)) {
            return 50000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la003.getValue(), loan)) {
            return 100000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la004.getValue(), loan)) {
            return 200000;
        }
        return 250000;
    }

    public static Integer getLoanTime(String loanTime) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt001.getValue(), loanTime)) {
            return 3;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt002.getValue(), loanTime)) {
            return 6;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt003.getValue(), loanTime)) {
            return 12;
        }
        return 24;
    }

    public static Integer getPurpose(String purpose) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt001.getValue(), purpose)) {
            return 1;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt002.getValue(), purpose)) {
            return 3;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt003.getValue(), purpose)) {
            return 4;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt004.getValue(), purpose)) {
            return 5;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt005.getValue(), purpose)) {
            return 6;
        }
        return 8;
    }

    public static Integer getShouRu(String shouru) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr001.getValue(), shouru)) {
            return 5000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr002.getValue(), shouru)) {
            return 10000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr003.getValue(), shouru)) {
            return 20000;
        }
        return 25000;
    }

    public static Integer getZhimaScore(String zhima) {
        if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm005.getValue(), zhima)) {
            return 720;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm004.getValue(), zhima)) {
            return 670;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm003.getValue(), zhima)) {
            return 620;
        }
        return 500;
    }

    /**
     * 加密
     *
     * @param data 待加密内容
     * @param key  加密key-24位长度
     * @param iv   加密iv-8位长度
     * @return 加密字符
     */
    public static String encrypt(String data, String key, String iv) {
        try {
            Cipher cipher = createCipher(iv, Cipher.ENCRYPT_MODE, key);
            return URLEncoder.encode(Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes())), String.valueOf(StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param data 待解密内容
     * @param key  加密key-24位长度
     * @param iv   加密iv-8为长度
     * @return 明文
     */
    public static String decrypt(String data, String key, String iv) {
        try {
            Cipher cipher = createCipher(iv, Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(Base64.getDecoder().decode(URLDecoder.decode(data, String.valueOf(StandardCharsets.UTF_8)))));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建密码器Cipher
     *
     * @param key  key
     * @param iv   加密iv
     * @param mode 加/解密模式
     * @return 密码器
     */
    private static Cipher createCipher(String iv, int mode, String key) {
        try {
            byte[] keyBytes = key.getBytes();
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
            cipher.init(mode, new SecretKeySpec(keyBytes, "DESede"), ivParameterSpec);
            return cipher;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
